//
//  WFChannelDetailCollectionViewCell.m
//  FAKER
//
//  Created by Jianwei on 15/11/4.
//  Copyright © 2015年 YHLai. All rights reserved.
//

#import "WFChannelDetailCollectionViewCell.h"
#import "FitHeader.h"
#import "WFChannelDetailImageCollectionViewCell.h"
#import "WFChannelDetailTitleLabelCollectionViewCell.h"
#import "WFModelSingleton.h"
#import "WFChannelWebViewController.h"
@implementation WFChannelDetailCollectionViewCell

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self createHeaderView];
        [self createItem];

    }
    return self;
}
- (void)createItem
{
    UICollectionViewFlowLayout *collectionViewFlowLayout = [[UICollectionViewFlowLayout alloc]init];
    collectionViewFlowLayout.minimumInteritemSpacing = .5;
    collectionViewFlowLayout.minimumLineSpacing = 1;

    
    
    
    _backCollectionView = [[BaseClassCollectionView alloc]initWithFrame:CGRectMake(0, 0, CONTENT_VIEW.WIDTH, CONTENT_VIEW.HEIGHT) collectionViewLayout:collectionViewFlowLayout];
    _backCollectionView.backgroundColor = [UIColor lightGrayColor];
    _backCollectionView.scrollEnabled = 0;
    _backCollectionView.dataSource = self;
    _backCollectionView.delegate = self;
    [_backCollectionView registerClass:[WFChannelDetailTitleLabelCollectionViewCell class] forCellWithReuseIdentifier:@"title"];
    
    [CONTENT_VIEW addSubview:_backCollectionView];
    
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{

        [self.delegate pushWFchannelWebViewControllerWithModel:_dataArray[indexPath.row]];
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    if (_dataArray.count) {
            if (indexPath.row == 0 && [_dataArray[0] media].count) {
                [_backCollectionView registerClass:[WFChannelDetailImageCollectionViewCell class] forCellWithReuseIdentifier:@"image"];
                WFChannelDetailImageCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"image" forIndexPath:indexPath];
                cell.model = _dataArray[indexPath.row];
                return cell;
            }
            else
            {
                WFChannelDetailTitleLabelCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"title" forIndexPath:indexPath];
                if (indexPath.row == 0) {
                    cell.contentView.backgroundColor = [UIColor RGBFromHexColor:[[WFModelSingleton shareInstance].model.block_info objectForKey:@"block_color"]];
                    if (NSUSERDEFAULTS_NIGHT) {
                        cell.contentView.backgroundColor = BGCOLOR_BLACK;
                        cell.titleLabel.textColor = [UIColor  lightGrayColor];
                    }
                }
                else
                {
                    if (NSUSERDEFAULTS_NIGHT) {
                        cell.contentView.backgroundColor = BGCOLOR_BLACK;
                        cell.titleLabel.textColor = [UIColor  lightGrayColor];
                    }
                    else
                    {
                        cell.contentView.backgroundColor = [UIColor whiteColor];
                        cell.titleLabel.textColor = [UIColor blackColor];
                        cell.auther_nameLabel.textColor = [UIColor grayColor];
                    }
                }
                cell.model = _dataArray[indexPath.row];
                return cell;
            }
    }
    return nil;
    
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return _dataArray.count;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    
    if (_dataArray.count) {
        if ([_dataArray[0] media].count)
        {
            CGFloat height = 210 * FITHEIGHT;
            if (indexPath.row == 0)
            {
                return CGSizeMake(CONTENT_VIEW.WIDTH, height);
            }
            else if(indexPath.row == 5)
                return CGSizeMake(CONTENT_VIEW.WIDTH , (CONTENT_VIEW.HEIGHT - height) / 3.0);
            else
            {
                return CGSizeMake(CONTENT_VIEW.WIDTH / 2 - .5, (CONTENT_VIEW.HEIGHT - height) / 3.0);
                
            }
        }
        else
        {
            if (indexPath.row == 0 || indexPath.row == 5)
            {
                return CGSizeMake(CONTENT_VIEW.WIDTH, CONTENT_VIEW.HEIGHT / 4.0);
            }
            else
            {
                return CGSizeMake(CONTENT_VIEW.WIDTH / 2 - .5, CONTENT_VIEW.HEIGHT / 4.0);
            }
        }
    }
    return CGSizeMake(0, 0);
}

- (void)createHeaderView
{
    BaseClassImageView *imageView = [[BaseClassImageView  alloc]initWithFrame:CGRectMake(127.5 * FITWIDTH, -83 * FITHEIGHT, 120 * FITWIDTH, 120 * FITWIDTH)];
    imageView.contentMode = 0;
    imageView.clipsToBounds = 0;
    [CONTENT_VIEW addSubview:imageView];
    [imageView sd_setImageWithURL:[NSURL URLWithString:[[WFModelSingleton shareInstance].model.block_info objectForKey:@"pic"]] placeholderImage:[UIImage imageNamed:@"PlaceHolder"] completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
        imageView.image = [image imageWithColor:[UIColor RGBFromHexColor:[[WFModelSingleton shareInstance].model.block_info objectForKey:@"block_color"]]];
        if (NSUSERDEFAULTS_NIGHT) {
            imageView.image = [image imageWithColor:[UIColor lightGrayColor]];
        }
    }];
    for (NSInteger i = 0; i < 2; i++) {
        UIView *view = [[UIView alloc]initWithFrame:CGRectMake((20 + 205 * i) * FITWIDTH, -20 * FITWIDTH, 130 * FITWIDTH, 1)];
        view.backgroundColor = [UIColor blackColor];
        [CONTENT_VIEW addSubview:view];
    }
}


- (void)setDataArray:(NSArray *)dataArray
{
    _dataArray = dataArray;
    [_backCollectionView reloadData];
    
}

@end
